package com.cmsr.hdpf.etl.task.parse

import com.alibaba.fastjson.JSON
import com.cmsr.hdpf.model.{SuzhouRoadMap, TrafficDataStream}

import scala.collection.mutable.{HashMap, ListBuffer, Map}

trait BaseParser {
    
    def Parse(rawJson: String): TrafficDataStream
    
    /**
     * 处理每个Participant
     *
     * @param jsonMap 一个交通参与者信息，类型为String
     * @return
     */
    protected def MapHandle(jsonMap: String): Map[String, String] = {
        var map = HashMap[String, String]()
        if (jsonMap != null && !jsonMap.isEmpty && jsonMap != "[]") {
            try {
                val jObject = JSON.parseObject(jsonMap)
                jObject.keySet().forEach(key => {
                    try {
                        map.put(key, jObject.getString(key))
                    } catch {
                        case x: Exception => println(key + "======>" + jsonMap)
                    }
                })
            }
            catch {
                case x: Exception => println("error" + x.getMessage)
            }
        }
        map
    }
    
    /**
     * 基类中的listHandle方法。将嵌套的List<Participant>中的每个元素都转为字符串，返回一个list，长度为交通参与者数目
     *
     * @param jsonMap
     * @return
     */
    protected def ListHandle(jsonMap: String): List[String] = {
        var list = ListBuffer[String]()
        if (jsonMap != null && !jsonMap.isEmpty && jsonMap != "[]") {
            try {
                val jsonArray = JSON.parseArray(jsonMap)
                jsonArray.forEach(p => {
                    list += p.toString
                }
                )
            } catch {
                case x: Exception => println("error" + x.getMessage)
            }
        }
        list.toList
    }
}